using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Friso.Entities;
using Friso.Business;
using FetCo.Data;
using FetCo.Framework;

public partial class Admin_CLipList : System.Web.UI.Page
{
    #region viewstate
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ClipAndImage._Table + "].[" + ClipAndImageColumns.CreateDate + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
   
    
    public DateTime CreatedDate
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["CreatedDate"] == null)
                return DateTime.Parse(FetConstant.DateDefault);
            else
                return Utilitys.ParseDateTime(HttpContext.Current.Request.QueryString["CreatedDate"]);
        }
    }
    
    public long MemberId
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["member"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["member"]);
        }
    }
    public string _Date
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["CreatedDate"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["CreatedDate"];
        }

    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["CurrentMenu"] = (int)Menus.Clip;
        Session["SubCurrentMenu"] = (int)SubMenus.ClipList;

        if (IsPostBack)
            return;
        LoadMember();
        LoadData();

    }
    protected void LoadMember()
    {
        Criteria cri = new Criteria();
        ddlMember.Items.Clear();
        ddlMember.Items.Add(new ListItem((string)Utilitys.Dictionary["option"], "0"));
        MemberCollection _MemberCollection = MemberService.GetMemberListByParam("[" + Member._Table + "].[" + MemberColumns.CreateDate + "]", OrderDirection.DESC, cri.Criter);
        foreach (Member _Member in _MemberCollection)
        {
            ddlMember.Items.Add(new ListItem(_Member.FullName, _Member.MemberID.ToString()));
        }

    }

    private string username = string.Empty;

    protected void LoadData()
    {

        Criteria cri = new Criteria();
        cri.add("[" + ClipAndImage._Table + "].[" + ClipAndImageColumns.IsClip + "]", 1, Condition.EQUAL, Condition.AND);
        if (this.MemberId != 0 && this.MemberId != long.MinValue)
        {
            cri.add("[" + ClipAndImage._Table + "].[" + ClipAndImageColumns.MemberId + "]", this.MemberId, Condition.EQUAL, Condition.AND);
        }
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        ClipAndImageCollection _ClipAndImageCollection = ClipAndImageService.GetClipAndImageListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        rptList.DataSource = _ClipAndImageCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;
    }
    protected void rptList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            
            ClipAndImage _ClipAndImage = (ClipAndImage)e.Item.DataItem;
            Literal ltrNo = (Literal)e.Item.FindControl("ltrNo");
            TextBox txtOrder = (TextBox)e.Item.FindControl("txtOrder");
            CheckBox ckbEnable = (CheckBox)e.Item.FindControl("ckbEnable");
            Literal ltrMember = (Literal)e.Item.FindControl("ltrMember");
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            Literal ltrNumVote = (Literal)e.Item.FindControl("ltrNumVote");
            ltrNo.Text = ltrNo.Text = Convert.ToString(NoCountStart + e.Item.ItemIndex);
            hidID.Value = _ClipAndImage.ClipId.ToString();
            ckbEnable.Checked = _ClipAndImage.Enable;
            Member _Member = MemberService.GetMember(_ClipAndImage.MemberId);
            if (_Member != null)
            {
                ltrMember.Text = _Member.FullName;
            }
            ltrNumVote.Text = _ClipAndImage.NumOfVote.ToString();
            

        }
    }

    protected void rptList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {

    }
    protected void lbtDelete_Onclick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HtmlInputCheckBox chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                try
                {
                    ClipAndImageService.DeleteClipAndImage(ID);
                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckbApprove = (CheckBox)item.FindControl("ckbApprove");
            CheckBox ckbEnable = (CheckBox)item.FindControl("ckbEnable");
            ClipAndImage _ClipAndImage = ClipAndImageService.GetClipAndImage(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");
            DropDownList ddlStatus = (DropDownList)item.FindControl("ddlStatus");
            if (_ClipAndImage != null)
            {
                _ClipAndImage.Order = int.Parse(txtOrder.Text.Trim());

                _ClipAndImage.Enable = ckbEnable.Checked;
                ClipAndImageService.UpdateClipAndImage(_ClipAndImage);
            }
        }
        LoadData();
    }
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("ClipList.aspx?member={0}", ddlMember.SelectedValue));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("ClipList.aspx");
    }
}
